/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.infra.topic.model;

import edu.cmu.lti.sepia.data.db.TaskDAO;

public class Task {

	private int taskId;
	private String taskName;
	private String taskNameShort;
	private String description;
	private boolean isMonolingual;
	private String srcLang;
	private String trgLang;
	private String[] types;
	private String[] tdLabels;
	private String[] irLabels;
	private String segmenterCmd;
	private String indices;
	private int currentPhase;
	
	public Task( int taskId, String taskName, String taskNameShort, String description, 
			int isMonolingual, String srcLang, String trgLang,  
			String types, String tdLabels, String irLabels, String segmenterCmd, 
			String indexPath, int currentPhase ) {
		this.taskId = taskId;
		this.taskName = taskName;
		this.taskNameShort = taskNameShort;
		this.description = description;
		this.isMonolingual = isMonolingual==1;
		this.srcLang = srcLang;
		this.trgLang = trgLang;
		this.types = convertToArray(types);
		this.tdLabels = convertToArray(tdLabels);
		this.irLabels = convertToArray(irLabels);
		this.segmenterCmd = segmenterCmd;
		this.indices = indexPath;
		this.currentPhase = currentPhase;
	}

	private static String[] convertToArray( String answerTypesDump ) {
	  if (answerTypesDump==null) return null;
		answerTypesDump = answerTypesDump.replaceAll("[;\\|]", ","); // normalize separator
		answerTypesDump = answerTypesDump.replaceAll(",$", ""); // remove last comma
		return answerTypesDump.split("\\s*,\\s*"); //split by comma (with possibly spaces around it)
	}
	
	private static String convertFromArray( String[] answerTypes ) {
		StringBuilder sb = new StringBuilder();
		for ( int i=0; i<answerTypes.length; i++ ) {
			sb.append( (i>0)?",":"" );
			sb.append( answerTypes[i] );
		}
		return sb.toString();
	}
	
	@Override
	public String toString() {
		try {
		  return TaskDAO.convertTask(this).toString();
    } catch (Exception e) {
      return e.getMessage();
    }
	};
	
	/**
	 * @return the types
	 */
	public String getTypesConcatenated() {
		return convertFromArray(types);
	}

  /**
   * @return the td labels (e.g. Y, N)
   */
  public String getTDLabelsConcatenated() {
    return convertFromArray(tdLabels);
  }

	/**
	 * @return the ir labels (e.g. L1, L2, L3)
	 */
	public String getIRLabelsConcatenated() {
	  return convertFromArray(irLabels);
	}
	
	/**
	 * @return the taskId
	 */
	public int getTaskId() {
		return taskId;
	}

	/**
	 * @return the taskName
	 */
	public String getTaskName() {
		return taskName;
	}

	/**
	 * @return the taskNameShort
	 */
	public String getTaskNameShort() {
		return taskNameShort;
	}
	/**
	 * @return the srcLang
	 */
	public String getSrcLang() {
		return srcLang;
	}
	/**
	 * @return the trgLang
	 */
	public String getTrgLang() {
		return trgLang;
	}

	/**
	 * @return the isMonolingual
	 */
	public boolean isMonolingual() {
		return isMonolingual;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the indices
	 */
	public String getIndices() {
		return indices;
	}
	/**
	 * @return the currentPhase
	 */
	public int getCurrentPhase() {
		return currentPhase;
	}

	/**
	 * @return the types
	 */
	public String[] getTypes() {
		return types;
	}

  /**
   * @return the tdLabels
   */
  public String[] getTdLabels() {
    return tdLabels;
  }

	/**
   * @return the irLabels
   */
  public String[] getIrLabels() {
    return irLabels;
  }
  
  /**
	 * @return the segmenterCmd
	 */
	public String getSegmenterCmd() {
		return segmenterCmd;
	}

	public String getFullName() {
	  String s = getSrcLang();
	  String t = getTrgLang();
	  if ( t==null || t.length()==0 ) {
	    return getTaskName();
	  } else if ( s.equals(t) || s ==null || s.length()==0) {
	    return getTaskName()+" ("+getTrgLang()+")";
	  } else {
	    return getTaskName()+" ("+getSrcLang()+"-"+getTrgLang()+")";
	  }
	}
	
	public static Task createEmpty() {
	  return new Task(0,null,null,null,0,null,null,null,null,null,null,null,0);
	}
	
}
